package com.qf.taobei.controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.qf.taobei.entity.*;
import com.qf.taobei.entity.vo.ViewProduct;
import com.qf.taobei.entity.vo.ViewProductType;
import com.qf.taobei.service.ProductService;
import com.qf.taobei.service.ProductTypeService;
import com.qf.taobei.utils.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
@RestController
@RequestMapping("/pro")
@Api(tags = "商品模块")
public class ProductController {
    @Autowired
    private ProductService productService;


    /**
     * 获取所有商品信息
     *
     * @return
     */
    @GetMapping("/queryAllProduct")
    @ApiOperation("获取商品列表")
    public RespBean queryAllProduct() {
        RespBean respBean = productService.queryAllProduct();
        return respBean;
    }

    /**
     * 获取单个商品信息(后端使用)
     *
     * @return
     */
    @GetMapping("/queryProductByIdForAfter")
    @ApiOperation("获取单个商品")
    public RespBean queryProductByIdForAfter(Integer id) {
        RespBean respBean = productService.queryProductByIdForAfter(id);
        return respBean;
    }

    /**
     * 获取单个商品信息(前端使用)
     *
     * @return
     */
    @GetMapping("/queryProductByIdForFront")
    @ApiOperation("获取单个商品")
    @JsonIgnoreProperties(value = "headller")
    public RespBean queryProductByIdForFront(HttpSession session) {
        RespBean respBean = productService.queryProductByIdForFront((Integer) session.getAttribute("pid"));
        return respBean;
    }

    @GetMapping("/queryProductByParentId")
    @ApiOperation("按类别查询商品")
    @JsonIgnoreProperties(value = "headller")
    public RespBean queryProductByParentId(Integer parentId) {
        RespBean respBean = productService.queryProductByParentId(parentId);
        return respBean;
    }

    /**
     * 通过商品ID删除商品
     *
     * @param id
     * @return
     */
    @PostMapping("/delProductById")
    @ApiOperation("删除商品")
    public RespBean delProductById(Long id) {
        RespBean respBean = productService.delProductById(id);
        return respBean;
    }

    /**
     * 通过商品ID批量删除商品
     *
     * @param id
     * @return
     */
    @PostMapping("/delProductByIdList")
    @ApiOperation("批量删除商品")
    public RespBean delProductByIdList(@RequestBody @RequestParam(value = "idList") List<Integer> idList) {
        RespBean respBean = productService.delProductByIdList(idList);
        return respBean;
    }


    /**
     * 根据商品ID修改商品信息
     *
     * @param product
     * @return
     */
    @PostMapping("/updateProductById")
    @ApiOperation("修改商品")
    public RespBean updateProductById(Product product) {
        RespBean respBean = productService.updateProductById(product);
        return respBean;
    }

    /**
     * 修改商品状态
     *
     * @param flag 设置的状态值  1 上架，2 下架， 3 审核中
     * @return
     */
    @PostMapping("/updateProductFlag")
    @ApiOperation("修改商品状态")
    public RespBean updateProductFlag(Integer id, Integer flag) {
        RespBean respBean = productService.updateProductFlag(id, flag);
        return respBean;
    }

    /**
     * 添加商品
     *
     * @param product
     * @return
     */
    @PostMapping("/addProduct")
    @ApiOperation("添加商品")
    public RespBean addProduct(Product product, HttpServletRequest request) {
        RespBean respBean = productService.addProduct( product, request);
        return respBean;
    }

    /**
     * 上传图片到服务器
     *
     * @param file
     * @param request
     * @return
     */
    //测试阿里云对象存储功能
    //回显图片
    @PostMapping(value = "/upload", consumes = "multipart/*",
            headers = "content-type=multipart/form-data")
    public RespBean upload(MultipartFile[] file, HttpServletRequest request) throws IOException {
        List urlList = new ArrayList();
        for (int i = 0; i < file.length; i++) {
            String imgUrl = OssUtils.upload(file[i].getOriginalFilename(), file[i].getInputStream());
            urlList.add(imgUrl);
        }
        //把图片路径集合存放在session中，添加商品的时候再存进数据库
        request.getSession().setAttribute("urlList", urlList);
        return RespBean.respSuccess("", urlList);
    }

    /**
     * 模糊查询
     *
     * @param dim
     * @return
     */
    @GetMapping("/queryProductByKeyword")
    @ApiOperation("模糊查询")
    public RespBean queryProductByKeyword(Dim dim) {
        RespBean respBean = productService.queryProductByDim(dim);
        return respBean;
    }

    /**
     * 根据店铺ID查询归属该店铺的商品
     *
     * @param request
     * @return
     */
    @GetMapping("/queryProductByStoreId")
    public RespBean queryProductByStoreId(HttpServletRequest request) {
        RespBean respBean = productService.queryProductByStoreId(request);
        return respBean;
    }

    //把商品ID存到session中，在展示商品详情时使用
    @PostMapping("/addProductId")
    public RespBean addProductId(HttpSession session, Integer id) {
        session.setAttribute("pid", id);
        return RespBean.respSuccess("存储成功");
    }

    //首页初始化展示商品
    @GetMapping("/getIndexProduct")
    public RespBean getIndexProduct() {
        //先查询商品一级分类
        List<ViewProductType> productTypeList = productService.queryProductByProductTypeId(1);
        ArrayList<ViewProductType> lists = new ArrayList<>(); //存放前台展示的数据
        //再根据一级分类查询他们下属所有商品
        ViewProductType viewProductType = null;
        for (int i = 0; i < productTypeList.size(); i++) {
            viewProductType = productTypeList.get(i);
            List<ViewProduct> viewProductList = productService.getProductByParentId(viewProductType.getId());
            if (viewProductList.size() >= 4 && lists.size() <= 4) {
                viewProductType.setProductList(viewProductList);
                lists.add(viewProductType);
            }
        }
        return RespBean.respSuccess("查询成功", lists);
    }

    /**
     * 根据商品一级分类的ID获取所有商品信息
     *
     * @param id 商品类型一级分类ID
     * @return
     */
    @GetMapping("/getProductListByTypeId")
    public RespBean getProductListByTypeId(Integer id) {
        //先根据类型ID查询商品类型信息
        ViewProductType viewProductType = productService.qeuryProductById(id);
        List<ViewProduct> viewProductList = productService.getProductByParentId(id);
        viewProductType.setProductList(viewProductList);
        return RespBean.respSuccess("成功", viewProductType);
    }
}


